package com.tender.aop.advice;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tender.properties.CommonProperties;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class MultiDataSourceAdvice {

    @Autowired
    private CommonProperties commonProperties;

    @Pointcut("@annotation(com.tender.aop.MultiDataSource)")
    public void pointcut() {
    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        // 获取方法名和参数
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        // 记录方法执行前
        log.info("【Before method】方法名称：{}, 具体参数：{}", methodName, JSON.toJSONString(args));
        // 执行目标方法
        Object result;

        // TODO 开始
        result = joinPoint.proceed();
        JSON.toJSONString(result);
        JSONObject.toJSONString(result);
        // TODO 结束
        // 记录方法执行后
        log.info("【After method】方法名称：{}, 执行结果：{}", methodName, JSON.toJSONString(result));

        return result;
    }

    /*
        if (ComConstants.ORACLE_DB.equalsIgnoreCase(visualProperties.getPriority())) {
            try {
                DynamicDataSourceContextHolder.push("visualization");
                DynamicTableNameHandler.setData();  // 动态表名
                result = joinPoint.proceed();
            } finally {
                DynamicDataSourceContextHolder.poll();
                DynamicTableNameHandler.clear();
            }
        } else {
            try {
                DynamicDataSourceContextHolder.push("visual");
                result = joinPoint.proceed();
            } finally {
                DynamicDataSourceContextHolder.poll();
            }
        }
     */

}
